#!/bin/bash

PROJECT_PATH=/root/project
PROJECT_NAME=donald-framework
GIT_PATH=https://gitee.com/donaldxie/donald-framework.git
#checking git version
git --version
if [ $? -ne 0 ]; then
  echo "git not found"
  echo "use apt installing git ..."
  apt install git
  if [ $? -ne 0 ]; then
      echo "apt install git is fail"
      exit 0
  fi
fi

#
cd ${PROJECT_PATH}

#删除原来的项目文件夹
clean
#clone项目
clone
#打包项目
package
#部署
deploy


clean(){
  rm -rf $PROJECT_NAME
}
clone(){
  echo "clone from git ... "

  git clone ${GIT_PATH}

  if [ $? -ne 0]; then
      echo "clone fail"
  fi
}

cd $PROJECT_PATH/$PROJECT_NAME

package(){
  mvn clean
  mvn install
}

deploy(){
  is_exist
  if [ $? -eq "0" ]; then
      echo "killing application pid=${pid}"
      kill -9 $pid
  fi
  nohup java -jar $PROJECT_PATH/$PROJECT_NAME/target/*.jar >> $PROJECT_PATH/$PROJECT_NAME/target/nohup &
  echo "successfully"
}

is_exist(){
  pid=`ps -ef|grep $PROJECT_NAME*.jar|grep -v grep|awk '{print $2}' `
  #如果不存在返回1，存在返回0
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
